package com.lgkj.ui.shop;

import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;

import com.bumptech.glide.Glide;
import com.lgkj.R;
import com.lgkj.utils.CommonConstant;
import com.lgkj.utils.CommonUtils;
import com.lgkj.utils.HttpUtils;
import com.lgkj.utils.TitleBuilder;
import com.lgkj.view.SelectPicPopupWindow;

import org.devio.takephoto.app.TakePhotoActivity;
import org.devio.takephoto.model.TResult;
import org.json.JSONObject;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by Administrator on 2018/8/20 0020.
 */

public class ShopCommentActivity extends TakePhotoActivity {
    @BindView(R.id.iv_photo)
    ImageView iv_photo;
    @BindView(R.id.iv_img)
    ImageView ivImg;
    @BindView(R.id.tb_item1)
    RadioButton tbItem1;
    @BindView(R.id.tb_item2)
    RadioButton tbItem2;
    @BindView(R.id.tb_item3)
    RadioButton tbItem3;
    @BindView(R.id.et_desc)
    EditText etDesc;
    private SelectPicPopupWindow menuWindow;
    private String imageId;
    private String img_url;
    private String goods_id = "230";
    private String goods_model_id = "127";
    private String score = "10";

    @Override
    public int getLayoutId() {
        return R.layout.activity_shop_comment;
    }

    @Override
    public void initView() {
        new TitleBuilder(ShopCommentActivity.this).setTitleText("商品评价").setLeftIcoListening(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    public void initData() {
        goods_id=getIntent().getStringExtra("goods_id");
        goods_model_id=getIntent().getStringExtra("goods_model_id");
        String url = getIntent().getStringExtra("image_url");
        Log.i(TAG, "initData: " + url);
        CommonUtils.LoadImage(ShopCommentActivity.this, ivImg, url, R.mipmap.defult_banner);
    }


    private View.OnClickListener itemsOnClick = new View.OnClickListener() {

        public void onClick(View v) {
            menuWindow.dismiss();
            switch (v.getId()) {
                case R.id.btn_take_photo:
                    CommonUtils.takeFromCapture(getTakePhoto(), 800, 800);
                    break;
                case R.id.btn_pick_photo:
                    CommonUtils.takeFromDocuments(getTakePhoto(), 800, 800);
                    //调用系统图库的意图
                    break;
                default:
                    break;
            }
        }
    };


    @Override
    public void takeCancel() {
        super.takeCancel();
    }

    @Override
    public void takeFail(TResult result, String msg) {
        super.takeFail(result, msg);
    }

    @Override
    public void takeSuccess(TResult result) {
        super.takeSuccess(result);
        String url = CommonConstant.getTakeImagePath();
        Log.i(TAG, "showImg: " + url);
        upload(new File(url));
        Glide.with(ShopCommentActivity.this).load(new File(url)).into(iv_photo);
    }

    private void upload(File file) {
        Map<String, String> map = new HashMap<>();
        map.put("access_token", CommonUtils.getToken(ShopCommentActivity.this));
        HttpUtils.uploadImage(map, CommonConstant.getUploadImg, file, new HttpUtils.HttpCallBack() {
            @Override
            public void successHttp(JSONObject jsonObject, int id) {
                dismissDiaLog();
                toastLong("上传成功", 0);
                imageId = jsonObject.optString("id");
            }

            @Override
            public void errorHttp(String msg) {
                toastLong("上传失败", 0);
            }
        });
    }

    private void save() {
        Map<String, String> map = new HashMap<>();
        map.put("access_token", CommonUtils.getToken(ShopCommentActivity.this));
        map.put("goods_id", goods_id);
        map.put("goods_model_id", goods_model_id);
        map.put("rate_content", etDesc.getText().toString());
        map.put("rate_score", score);
        if (imageId.equals("")) {
            toastLong("请先上传图片", 0);
            return;
        }
//        http://mdyt.lianguangtech.com/api/rate/addRate?rate_content=哦一直在&goods_id=230&goods_model_id=127&sid=rct7s153m2ka6bpvm4ca57tbk6&photos=267&rate_score=5&access_token=7e4fb28393549676d8b392ce3dde336e
        map.put("photos", imageId);
        HttpUtils.postHttp(map, CommonConstant.addRate, new HttpUtils.HttpCallBack() {
            @Override
            public void successHttp(JSONObject jsonObject, int id) {
                toastLong(jsonObject.optString("msg"), 0);
                if (jsonObject.optInt("code") == 0) {
                    finish();
//                    Glide.with(ShopCommentActivity.this).load(new File(CommonConstant.getTakeImagePath())).into(iv_photo);
//                    initDatas();
                }
            }

            @Override
            public void errorHttp(String msg) {

            }
        });
    }


    @OnClick({R.id.iv_photo, R.id.iv_save, R.id.tb_item1, R.id.tb_item2, R.id.tb_item3})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_photo:
                menuWindow = new SelectPicPopupWindow(ShopCommentActivity.this, itemsOnClick);
//        menuWindow.sethideTake();
                //显示窗口
                menuWindow.showAtLocation(ShopCommentActivity.this.findViewById(R.id.main), Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
                break;
            case R.id.iv_save:
                save();
                break;
            case R.id.tb_item1:
                score = "10";
                break;
            case R.id.tb_item2:
                score = "5";
                break;
            case R.id.tb_item3:
                score = "0";
                break;
        }
    }
}
